// ViewDataActivity.java
package com.example.qrcodecamera;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

public class ViewDataActivity extends AppCompatActivity {

    private static final int PAGE_SIZE = 10; // 每页显示10条
    private int currentPage = 1;
    private int totalRecords = 0;

    private TableLayout tableLayout;
    private Button btnPrev, btnNext;
    private TextView tvPageInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_data);

        // 初始化视图
        tableLayout = findViewById(R.id.table_data);
        btnPrev = findViewById(R.id.btn_prev);
        btnNext = findViewById(R.id.btn_next);
        tvPageInfo = findViewById(R.id.tv_page_info);

        // 初始化数据库
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        totalRecords = dbHelper.getTotalCount();

        // 设置按钮监听
        btnPrev.setOnClickListener(v -> loadPage(currentPage - 1));
        btnNext.setOnClickListener(v -> loadPage(currentPage + 1));

        // 加载第一页
        loadPage(1);
    }

    private void loadPage(int page) {
        // 边界检查
        if (page < 1 || page > getTotalPages()) return;

        currentPage = page;
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        List<WeightRecord> records = dbHelper.getPagedData(page, PAGE_SIZE);

        // 清空表格
        tableLayout.removeAllViews();

        // 填充数据
        for (WeightRecord record : records) {
            TableRow row = new TableRow(this);
            row.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));

            addTextView(row, record.getDate());
            addTextView(row, record.getBatchNumber());
            addTextView(row, record.getNumber());
            addTextView(row, record.getWeight1());
            addTextView(row, record.getWeight2());
            addTextView(row, record.getWeight3());

            tableLayout.addView(row);
        }

        // 更新分页状态
        updatePaginationUI();
    }

    private void addTextView(TableRow row, String text) {
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setPadding(8, 8, 8, 8);
        tv.setLayoutParams(new TableRow.LayoutParams(
                0,
                TableRow.LayoutParams.WRAP_CONTENT,
                1f));
        row.addView(tv);
    }

    private int getTotalPages() {
        return (int) Math.ceil((double) totalRecords / PAGE_SIZE);
    }

    private void updatePaginationUI() {
        // 更新页码显示
        tvPageInfo.setText(String.format("第%d页/共%d页", currentPage, getTotalPages()));

        // 控制按钮状态
        btnPrev.setEnabled(currentPage > 1);
        btnNext.setEnabled(currentPage < getTotalPages());
    }
}
